package com.fg.pro.trade.utils;

import cn.hutool.core.util.StrUtil;
import com.fg.pro.trade.entity.TradeSale;
import com.fg.pro.trade.service.TradeCustomerService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.fg.pro.trade.constant.RedissonConst.REDIS_CACHED_SIMULATION;

@Component
public class TradeGoodsSimulationUtils {

    private final static String SIMULATION_GOODS_KEY = REDIS_CACHED_SIMULATION+"goods";

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    TradeCustomerService  tradeCustomerService;

    public List<String> getAllowCustomerLine(){
        List<String> customerLine = stringRedisTemplate.opsForList().range(SIMULATION_GOODS_KEY,0,-1);
        if(!customerLine.isEmpty()){
            return customerLine;
        }
        List<String> list = tradeCustomerService.query().groupBy("customer_route").list().stream().filter(item -> StrUtil.isNotEmpty(item.getCustomerRoute())).map(item -> {
            return item.getCustomerRoute();
        }).collect(Collectors.toList());
//        List<String> list = new ArrayList<>();
        stringRedisTemplate.opsForList().leftPushAll(SIMULATION_GOODS_KEY,list);
        return list;
    }

    public boolean saveAllowCustomerLine(String customerLine){
        List<String> allowCustomerLine = getAllowCustomerLine();
        boolean save = allowCustomerLine.add(customerLine);
        stringRedisTemplate.opsForList().leftPushAll(SIMULATION_GOODS_KEY,allowCustomerLine);
        return save;
    }

    public boolean delAllowCustomerLine(String customerLine){
        List<String> allowCustomerLine = getAllowCustomerLine();
        boolean remove = allowCustomerLine.remove(customerLine);
        stringRedisTemplate.opsForList().leftPushAll(SIMULATION_GOODS_KEY,allowCustomerLine);
        return remove;
    }

    public boolean saveAllAllowCustomerLine(List<String> customerLines) {
        Boolean delete = stringRedisTemplate.delete(SIMULATION_GOODS_KEY);
        stringRedisTemplate.opsForList().leftPushAll(SIMULATION_GOODS_KEY,customerLines);
        return true;
    }


}
